home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / umount.c < prev    next >
C/C++ Source or Header  |  1990-07-15  |  1KB  |  75 lines

  1. /* umount - unmount a file system        Author: Andy Tanenbaum */
  2.  
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6.  
  7. #define BUFSIZE 1024
  8.  
  9. extern int errno;
  10. char *mounttable = "/etc/mtab";
  11. char buffer[BUFSIZE], *p = &buffer[0], *q;
  12.  
  13. main(argc, argv)
  14. int argc;
  15. char *argv[];
  16. {
  17.  
  18.   if (argc != 2) usage();
  19.   if (umount(argv[1]) < 0) {
  20.     if (errno == EINVAL)
  21.         std_err("Device not mounted\n");
  22.     else
  23.         perror("umount");
  24.     exit(1);
  25.   }
  26.   std_err(argv[1]);
  27.   std_err(" unmounted\n");
  28.   do_mtab(argv[1]);
  29.   exit(0);
  30. }
  31.  
  32. do_mtab(devname)
  33. char *devname;
  34. {
  35. /* Remove an entry from mtab. */
  36.   int n, fd;
  37.   char line[256];
  38.  
  39.   /* Read in the mount table and then overwrite the file. */
  40.   fd = open(mounttable, O_RDWR);
  41.   n = read(fd, buffer, BUFSIZE);
  42.   close(fd);
  43.   q = &buffer[n];
  44.   fd = creat(mounttable, 0554);
  45.  
  46.   n = strlen(devname);
  47.   while (getline(line) != 0) {
  48.     if (strncmp(line, devname, n) == 0) continue;
  49.     write(fd, line, strlen(line));
  50.   }
  51. }
  52.  
  53. int getline(ptr)
  54. char *ptr;
  55. {
  56.   char c;
  57.  
  58.   while (p < q) {
  59.     c = *p++;
  60.     *ptr++ = c;
  61.     if (c == '\n') {
  62.         *ptr++ = 0;
  63.         return(1);
  64.     }
  65.   }
  66.   return(0);
  67. }
  68.  
  69.  
  70. usage()
  71. {
  72.   std_err("Usage: umount special\n");
  73.   exit(1);
  74. }
  75.